#!/usr/bin/env python

import datetime
import wsgiref.handlers
import os

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template 

class Note(db.Model):
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)


class MainPage(webapp.RequestHandler):
  def get(self):
    notes = db.GqlQuery("SELECT * FROM Note ORDER BY date DESC")

    template_values = {
		    'notes': notes,
		    }

    path = os.path.join(os.path.dirname(__file__), 'main.html')
    self.response.out.write(template.render(path, template_values))


class SaveNote(webapp.RequestHandler):
  def post(self):
    note = Note()
    note.content = self.request.get('content')
    note.put()
    self.redirect('/')


application = webapp.WSGIApplication([
  ('/', MainPage),
  ('/sign', SaveNote)
], debug=True)


def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
